package com.blockmeta.bbs.immodule.bean;


import com.blankj.utilcode.util.LogUtils;

import com.blockmeta.bbs.immodule.dao.ChatMessage;

import buf.Msg;

public class MsgHelper {
    /**
     * 生成一个带返回消息的消息
     *
     * @param type
     * @param message
     * @return
     */
    public static Msg.Message newResultMessage(Msg.MessageType type, String message) {
        Msg.ResultMessage.Builder builder = Msg.ResultMessage.newBuilder();
        Msg.ResultMessage rtMessage = builder.setMessage(message)
                .setMessageType(type).build();
        Msg.Message.Builder b = Msg.Message.newBuilder();
        Msg.Message m = b.setResultMessage(rtMessage).setMessageType(type)
                .build();
        return m;
    }

    /**
     * 生成一个带ping/pong消息的消息
     * ping/pong 表示心跳连接
     *
     * @param type
     * @param clientId
     * @return
     */
    public static Msg.Message newPingMessage(Msg.MessageType type, String clientId) {
        LogUtils.d("author" + "im ping" + clientId);
        Msg.PingMessage.Builder bu = Msg.PingMessage.newBuilder();
        Msg.PingMessage rtMessage = bu.setClientId(clientId)
                .setMessageType(type).build();
        Msg.Message.Builder b = Msg.Message.newBuilder();
        Msg.Message m = b.setPingMessage(rtMessage).setMessageType(type)
                .build();
        return m;
    }

    /**
     * 单人聊天消息
     *
     * @param fromId
     * @param toId
     * @param content
     * @param token
     * @param date
     * @param contentType
     * @param param
     * @param userName
     * @param groupId
     * @return
     */
    public static Msg.Message newUUChatMessage(String uuid, int fromId, long toId,
                                               String content, String token, boolean transfer, int date,
                                               int id, int contentType, String path,
                                               int status, String fileName, int fileType, String fileParam
            , String userName, String userAvator, int groupId) {
        Msg.ChatMessage chatMessage = Msg.ChatMessage.newBuilder()
                .setContent(content).setFromId(fromId).setToId(toId)
                .setMsgType(ChatMessage.MSG_TYPE_UU).setToken(token)
                .setChatMessageId(id).setDate(date).setTransfer(transfer)
                .setFilePath(path)
                .setStatus(status).setUuid(uuid).setContentType(contentType)
                .setFileName(fileName)
                .setFileType(fileType)
                .setFileParam(fileParam)
                .setUsername(userName)
                .setUseravator(userAvator)
                .setUserGroupId(groupId)
                .build();

        Msg.Message.Builder b = Msg.Message.newBuilder();
        Msg.Message m = b.setChatMessage(chatMessage)
                .setMessageType(Msg.MessageType.CHAT_MESSAGE).build();

//        LogUtils.i("author" +"Socket发送单人聊天了:"+chatMessage.toString());
        return m;
    }

    /**
     * 群聊天
     *
     * @param fromId
     * @param chatGroupId
     * @param content
     * @param token
     * @param transfer
     * @param date
     * @param id
     * @param contentType 消息类型 文字 附件 等等
     * @param path
     * @param param
     * @param userName
     * @param groupId
     * @return
     */
    public static Msg.Message newUCGChatMessage(String uuid, int fromId,
                                                long chatGroupId, String content, String token, boolean transfer,
                                                int date, int id, int contentType,
                                                String path, int status, String fileName, int fileType, String fileParam
            , String userName, String userAvator, int groupId) {
        Msg.ChatMessage chatMessage = Msg.ChatMessage.newBuilder()
                .setContent(content).setFromId(fromId)
                .setChatGroupId(chatGroupId)
                .setMsgType(ChatMessage.MSG_TYPE_UCG).setToken(token)
                .setChatMessageId(id).setDate(date).setTransfer(transfer)
                .setFilePath(path)
                .setUuid(uuid).setStatus(status).setContentType(contentType)
                .setFileName(fileName)
                .setFileType(fileType)
                .setFileParam(fileParam)
                .setUsername(userName)
                .setUseravator(userAvator)
                .setUserGroupId(groupId)
                .build();

        Msg.Message.Builder b = Msg.Message.newBuilder();
        Msg.Message m = b.setChatMessage(chatMessage)
                .setMessageType(Msg.MessageType.CHAT_MESSAGE).build();
//        LogUtils.i("author" +"Socket发送群聊天了:"+chatMessage.toString());

        return m;
    }


    /**
     * @param type
     * @param userId
     * @param para   可以为空
     * @return
     */
    public static Msg.Message newClientRequestMessage(int type, int userId,
                                                      String token, String para) {
        Msg.ClientRequestMessage.Builder bu = Msg.ClientRequestMessage
                .newBuilder();
        Msg.ClientRequestMessage rtMessage = bu.setUserId(userId)
                .setToken(token).setRequestType(type).setParameter(para)
                .build();
        Msg.Message.Builder b = Msg.Message.newBuilder();
        Msg.Message m = b.setClientRequestMessage(rtMessage)
                .setMessageType(Msg.MessageType.CLIENT_REQUEST).build();
        return m;
    }

    /**
     * 文件上传类
     * 文件上传不采用Socket
     * 采用Http上传到七牛云，然后告知服务器
     *
     * @param file
     * @param userId
     * @param token
     * @return
     */

    /**
     * 消息回执
     *
     * @param uuid
     * @param status
     * @param userId
     * @param token
     * @return
     */
    public static Msg.Message newReceiptMessage(String uuid, int status,
                                                int userId, String token) {
        Msg.Message.Builder b = Msg.Message.newBuilder();

        Msg.ReceiptMessage.Builder rb = Msg.ReceiptMessage.newBuilder();
        rb.setStatus(status).setUuid(uuid).setUserId(userId).setToken(token);

        Msg.ReceiptMessage rm = rb.build();

        b.setMessageType(Msg.MessageType.RECEIPT);
        b.setReceiptMessage(rm);
        Msg.Message m = b.build();
        return m;
    }
}
